package com.aaa.hospital.ManagementEnd.controller;

import com.aaa.hospital.ManagementEnd.pojo.Check;
import com.aaa.hospital.ManagementEnd.pojo.Dispensary;
import com.aaa.hospital.ManagementEnd.pojo.Medicine;
import com.aaa.hospital.ManagementEnd.service.Impl.CheckSerImpl;
import com.aaa.hospital.ManagementEnd.service.Impl.DispensarySerImpl;
import com.aaa.hospital.ManagementEnd.service.Impl.MedicinImpl;
import com.aaa.hospital.ManagementEnd.service.RegistrationServices;
import com.aaa.hospital.ManagementEnd.util.MedicineVo;
import com.aaa.hospital.ManagementEnd.util.RegistrationDB;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

///lgq
@Controller
@RequestMapping("/regist")
public class RegistrationController {
    //预约
    @Autowired
    private RegistrationServices services;
    //药房
    @Autowired
    private DispensarySerImpl dispensarySerImpl;
    //药品
//    @Autowired
//    private CheckSerImpl checkSerImpl;
    @Autowired
    private MedicinImpl medicinImpl;


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public String queryAll(HttpSession session) {
        String user = (String) session.getAttribute("user");
        Integer did = new Integer(user);
        JSONObject json = new JSONObject();
        List<RegistrationDB> datt = services.queryAll(did);
        Object objData = JSONObject.toJSON(datt);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count", datt.size());
        json.put("data", objData);
        return json.toJSONString();
    }

    @RequestMapping("/limitAll")
    @ResponseBody
    public String limitAll(@Param("visid") Integer visid, @Param("uname") String uname) {
        JSONObject json = new JSONObject();
        List<RegistrationDB> registrationDBS = services.limitAll(10001, visid, uname);
        Object objData = JSONObject.toJSON(registrationDBS);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count", registrationDBS.size());
        json.put("data", objData);
        return json.toJSONString();
    }


    /*选择患者，获取患者id，传到后台，通过后台查询患者个人信息，
     *主治医生个人信息，
     * 并携带这些信息跳转到新的页面，
     * */
    @RequestMapping("/page")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    public String page(@Param("uid") Integer uid, Model model) {
        services.update_state(uid, 1);
        RegistrationDB regDBS = services.selectById(uid);
        model.addAttribute("dbs", regDBS);
        return "ManagementEnd/Home/cure";
    }

    @RequestMapping("/dispensaryAll")
    @ResponseBody
    public List<Dispensary> dispensaryAll() {
        List<Dispensary> dispensaries = dispensarySerImpl.queryAll();
        return dispensaries;
    }


    @RequestMapping("/checkSerById")
    @ResponseBody
    public List<Medicine> checkSerById(Integer yfid) {

        return medicinImpl.queryById(yfid);
    }


    @RequestMapping("/checkById")
    @ResponseBody
    public MedicineVo checkById(Integer ypid) {
        return medicinImpl.selectById(ypid);
    }


}
